package com.hm.service.config.opencv;

import org.opencv.objdetect.CascadeClassifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpencvConfig {
    private static final Logger logger = LoggerFactory.getLogger(OpencvConfig.class);

    @Bean(name = {"OpencvConfig"})
    public CascadeClassifier cascadeClassifier() {
        String osName = System.getProperty("os.name").toLowerCase();
        logger.warn("=======>opencv初始化了<=======该系统是:{}", osName);
        try {
            if (osName.contains("win")) {
                return initOpenCVToWin();
            } else if (osName.contains("nux")) {
                return initOpenCVToLinux();
            } else {
                logger.warn("不支持该系统！");
                return null;
            }
        } catch (Exception e) {
            logger.warn("opencv初始化异常：{}", e.getMessage());
            return null;
        }
    }

    private static CascadeClassifier initOpenCVToWin() {
        //加载动态链接库
        String absolutePath = "C:/support-library/opencv_java4100.dll";
        System.load(absolutePath);
        //加载人脸探测器
        CascadeClassifier cascadeClassifier = new CascadeClassifier("C:/support-library/haarcascade_frontalface_alt.xml");
        logger.warn("opencv初始化成功!-win");
        return cascadeClassifier;
    }

    private static CascadeClassifier initOpenCVToLinux() {
        //加载动态链接库
        String absolutePath = "/support-library/libopencv_java4.so";
        System.load(absolutePath);
        //加载人脸探测器
        CascadeClassifier cascadeClassifier = new CascadeClassifier("/support-library/haarcascade_frontalface_alt.xml");
        logger.warn("opencv初始化成功!-linux");
        return cascadeClassifier;
    }
}
